using HarmonyLib;
using System.Reflection;
using Duckov.Modding;

namespace CameraChange
{
    public class ModBehaviour : Duckov.Modding.ModBehaviour
    {
        private const string ID = "Patchs.CameraChange";
        private Harmony? _harmony;

        private void OnEnable()
        {
            // 初始化所有静态反射
            if (!AutoReflection.IsInitialized()) return;
            
            // 订阅其它Mod加载成功事件
            ModManager.OnModActivated += OnModActivated;

            _harmony = new Harmony(ID);
            _harmony.PatchAll(Assembly.GetExecutingAssembly());
        }

        private void OnDisable()
        {
            ModManager.OnModActivated -= OnModActivated;

            _harmony?.UnpatchAll(ID);
        }


        // 程序退出时保存配置
        public void OnApplicationQuit()
        {
            AutoConfigManager.SaveAllConfigs();
        }

        
        // 在ModSetting加载之前加载时等待其加载
        private void OnModActivated(
            ModInfo activatedModInfo,
            Duckov.Modding.ModBehaviour modBehaviour
        )
        {
            // 检查启用的mod是否是ModSetting, 是则进行初始化
            if (activatedModInfo.name == "ModSetting")
                AutoConfigManager.InitializeAllConfigs(this);
        }

        // 在ModSetting加载之后加载时初始化
        protected override void OnAfterSetup()
        {
            // 尝试初始化自动化配置系统
            AutoConfigManager.InitializeAllConfigs(this);
        }
    }
}